<template>
  <div id="app">
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>机票列表</span>
              <el-input placeholder="请输入机票编号" v-model="search.id"/>
              <el-input placeholder="请输旅客证件号" v-model="search.idCard"/>
              <el-select v-model="search.flightId" placeholder="请选择机票班次">
                <el-option
                    v-for="item in flights"
                    :key="item.flightId"
                    :label="item.flightId"
                    :value="item.flightId">
                </el-option>
              </el-select>
              <el-button type="primary" @click="handleSelectTicket">查询机票</el-button>
            </div>
          </template>
          <el-table :data="tickets" border style="width: 100%">
            <el-table-column prop="id" label="机票编号">
            </el-table-column>
            <el-table-column prop="flightId" label="班次号">
            </el-table-column>
            <el-table-column prop="idCard" label="旅客证件号">
            </el-table-column>
            <el-table-column prop="idCardType" label="旅客证件类型">
            </el-table-column>
            <el-table-column prop="status" label="状态">
            </el-table-column>
            <el-table-column prop="seatNumber" label="座位号">
            </el-table-column>
            <el-table-column prop="orderId" label="订单号">
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button type="primary" size="small" @click="handleEditTicket(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-pagination
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-size="pageSize"
              :total="totalElements"
              layout="prev, pager, next, jumper"
          />
        </el-card>
      </el-col>
    </el-row>

    <el-dialog v-model="editDialogVisible" title="添加机票" width="50%">
      <el-form :model="form" label-width="150px">
        <el-form-item label="机票编号">
          <el-input disabled v-model="form.id" />
        </el-form-item>
        <el-form-item label="班次号">
          <el-input disabled v-model="form.flightId" />
        </el-form-item>
        <el-form-item label="旅客证件号">
          <el-input v-model="form.idCard" />
        </el-form-item>
        <el-form-item label="旅客证件类型">
          <el-input v-model="form.idCardType" />
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="form.status" />
        </el-form-item>
        <el-form-item label="座位号">
          <el-input disabled v-model="form.seatNumber" />
        </el-form-item>
        <el-form-item label="订单号">
          <el-input disabled v-model="form.orderId" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveTicket">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'AdminTicket',
  data() {
    return {
      tickets: [],
      flights: [],
      search:{
        id: '',
        flightId: '',
        idCard: '',
      },
      editDialogVisible: false,
      form: {
        id: '',
        flightId: '',
        idCard: '',
        idCardType: '',
        status: '',
        seatNumber: '',
        orderId: '',
      },
      currentPage: 1, // 当前页码
      pageSize: 20, // 每页数据量
      totalElements: 0, // 总数据量
    };
  },
  mounted() {
    this.fetchFlights();
    this.fetchTickets();
  },
  methods: {
    handleSelectTicket() {
      this.currentPage = 1; // 重置页码
      this.fetchTickets();
    },
    fetchTickets(){
      axios.get('/tickets', {
        params: {
          ...this.search,
          page: this.currentPage - 1, // 将当前页码转换为 0-based index
          size: this.pageSize,
        },
      })
          .then(response => {
            this.tickets = response.data.content;
            this.currentPage = response.data.number + 1; // 更新当前页码
            this.pageSize = response.data.size; // 更新每页数据量
            this.totalElements = response.data.totalElements; // 更新总数据量
          })
          .catch(error => {
            console.error('获取机票列表失败', error);
            ElMessage.error('获取机票列表失败');
          });
    },
    fetchFlights(){
      axios.get('/flight')
          .then(response => {
            this.flights = response.data;
          })
          .catch(error => {
            console.error('获取航班列表失败', error);
            ElMessage.error('获取航班列表失败');
          });
    },
    handleAddTicket() {
      this.dialogVisible = true;
      this.form = {
        id: '',
        flightId: '',
        idCard: '',
        idCardType: '',
        status: '',
        seatNumber: '',
        orderId: '',
      };
    },
    handleSaveTicket() {
      axios.post('/tickets', this.form)
          .then(() => {
            this.dialogVisible = false;
            this.fetchTickets();
            ElMessage.success('更新机票成功');
          })
          .catch(error => {
            console.error('更新机票失败', error);
            ElMessage.error('更新机票失败');
          });
    },
    handleEditTicket(ticket) {
      this.form = ticket;
      this.editDialogVisible = true;
      console.log('编辑机票', ticket);
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.fetchTickets();
    },
  },
};
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
